<script setup>
import { defineEmits } from 'vue';
import { ref, toRef, toRefs, reactive, onMounted } from 'vue';
import axios from 'axios';
const emits = defineEmits(['login'])
import { Base64 } from 'js-base64';
import { Text } from 'vue';
import CryptoJS from 'crypto-js';

let form = ref(false);
let id = ref("");
var password = ref("");
let pwshow = ref(false);
var isloading = ref(false);
let error=ref(false)
const required = (value) => !!value || '必填.';

const login = async (id, password) => {
    let info = {}
    //验证正确
    console.log(password,typeof(password))
    let binaryData = password.value.split('').map(c => c.charCodeAt(0)); // 将字符串转换为二进制数据
    let base64Data = Base64.encode(binaryData);
    let err=false
    console.log(base64Data);
    await axios({
        method: 'get',
        url: 'http://127.0.0.1:8000/login',
        params: {
            "id": id.value,
            "password": base64Data
        }
    }).then(res => {
        console.log(res)
        if (res.data.result == "success") {
            info = res.data.data
            console.log("info", info)
            emits('login', info)
        }
        else
        {
            err=true
        }
    })
    if(err)
    {
        error.value=true
    }
    //info=getinfo(id)
    // let role=""
    // id=id.value
    // if(id.substring(0,2)=="00")
    // {
    //     role="admin"
    // }
    // else if(id.substring(0,2)=="10")
    // {
    //     role="teacher"
    // }
    // else
    // {
    //     role="stu"
    // }
    // let info = {
    //     name: "张三",
    //     id: 21121111,
    //     school: "计算机学院",
    //     role:role
    // }
    //登陆
}
const onSubmit = () => {

    isloading = true

    login(id, password)

    setTimeout(() => (isloading = false), 2000)
}

</script>

<template>
    <v-container>
        <v-card class="text-h3 py-10 text-center">欢迎访问教务管理系统</v-card>
        <v-sheet class="mx-auto px-6 py-8" max-width="360">
            <v-container align-self="center">
                <v-form v-model="form" @submit.prevent="onSubmit">
                    <v-text-field v-model="id" :readonly="loading" :rules="[required]" class="mb-2" label="学工号"
                        placeholder="请输入学工号"></v-text-field>

                    <v-text-field v-model="password" :append-inner-icon="pwshow ? 'mdi-eye' : 'mdi-eye-off'" :rules="[required]"
                        :type="pwshow ? 'text' : 'password'" name="input-10-1" label="密码"
                        @click:append-inner="pwshow = !pwshow"></v-text-field>
                    <br>

                    <v-btn :disabled="!form" :loading="loading" block color="success" size="large" type="submit"
                        variant="elevated">
                        Sign In
                    </v-btn>
                    <v-card v-if="error" style="color: red;">用户名或密码错误</v-card>
                </v-form>
            </v-container>
        </v-sheet>
    </v-container>
</template>

